628C - Bear and String Distance - CodeForces Solution


greedy strings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<< #x <<" : "<<x<<endl;
#define dbgv(v) cout<< #v <<" : ";for(auto &i:v) cout<<i<<" ";cout<<endl;
#define endl '\n'
const int MOD = 1e9+ 7;

int main() {
	ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
	int T = 1,i;
	//cin>>T;
	while(T--) {
    	//string S;
    	//cin>>S;
    	int N,K;
    	cin>>N>>K;
    	string S;
    	cin>>S;
    	string V="";
    	for(i=0;i<N;i++)
    	{
    		if(K>0)
    		{
    			int P=max(abs(S[i]-'a'),abs(S[i]-'z'));
    			if(P<K)
    			{
    				if(P==abs(S[i]-'a'))
    				{
    					S[i]='a';
    				}
    				else
    				{
    					S[i]='z';
    				}
    				K=K-P;
    			}
    			else if(P==K)
    			{
    				if(P==abs(S[i]-'a'))
    				{
    					S[i]='a';
    				}
    				else
    				{
    					S[i]='z';
    				}
    				K=0;
    			}
    			else
    			{
    				if(S[i]+K<='z')
    				{
    					S[i]=S[i]+K;
    					K=0;
    				}
    				else
    				{
    					S[i]=S[i]-K;
    					K=0;
    				}
    			}
    		}
    		if(K==0)
    		{
    			break;
    		}
    	}
    	//cout<<K<<endl;
    	if(K==0)
    	{
    		cout<<S<<endl;
    	}
    	else
    	{
    		cout<<-1<<endl;
    	}
	}
}


Comments

Submit
0 Comments
More Questions

1409B - Minimum Product
723B - Text Document Analysis
1471C - Strange Birthday Party
1199A - City Day
1334A - Level Statistics
67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers